namespace Example.Controllers
{
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Example.Models;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class GreetingControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<GreetingController>()
                .DerivesFrom<ResourceController<Greeting>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new GreetingController());
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Greeting>(new
                {
                    urn = "urn://simplewebservices.org/translation/greeting",
                    xml =
                        "<greeting>" +
                            "<value>" +
                                "<translation xml:lang='en'>" +
                                    "<string>Hello</string>" +
                                "</translation>" +
                                "<translation xml:lang='fr'>" +
                                    "<string>Bonjour</string>" +
                                "</translation>" +
                            "</value>" +
                        "</greeting>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/greeting.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                GreetingController controller = new GreetingController();
                controller.ControllerContext = new ControllerContext(mock.Object, new RouteData(), controller);

                Assert.IsAssignableFrom<ViewResult>(controller.HtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Greeting>(new
                {
                    urn = "urn://simplewebservices.org/translation/greeting",
                    xml = 
                        "<greeting>" +
                            "<value>" +
                                "<translation xml:lang='en'>" +
                                    "<string>Hello</string>" +
                                "</translation>" +
                                "<translation xml:lang='fr'>" +
                                    "<string>Bonjour</string>" +
                                "</translation>" +
                            "</value>" +
                        "</greeting>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/greeting.en.xhtml").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                GreetingController controller = new GreetingController();
                controller.ControllerContext = new ControllerContext(mock.Object, new RouteData(), controller);

                Assert.IsAssignableFrom<ViewResult>(controller.XhtmlRepresentation(new CultureInfo("en")));

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}